//
//  addLeaderView.m
//  HappyStore
//
//  Created by 点点充 on 16/1/28.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "addLeaderView.h"

@interface addLeaderView ()<UIGestureRecognizerDelegate>
{
    BOOL selectStartAndEndDate;
    
    UIButton *btnAddLogo;
    UIImage *imgSelected;
    
}

@property (nonatomic, strong) UITextField *tf1;
@property (nonatomic, strong) UITextField *tf2;
@property (nonatomic, strong) UIViewController *mainVC;

@end

@implementation addLeaderView
@synthesize tf1,tf2;

+ (addLeaderView *)sharedSelectedAddress
{
    static addLeaderView *_sharedService = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedService = [[addLeaderView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _sharedService.backgroundColor = [[UIColor colorWithWhite:0.6 alpha:1.0f] colorWithAlphaComponent:0.7];
    });
    
    
    return _sharedService;
}

#pragma mark - 添加教育经历
- (void)addLeaderWith:(UIViewController *)vc
{
    self.mainVC = vc;
    self.hidden = NO;
    //加入手势来取消键盘
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
                                             initWithTarget:self action:@selector(dismissView)];
    tapRecognizer.cancelsTouchesInView = NO;
    tapRecognizer.delegate = self;
    [self addGestureRecognizer:tapRecognizer];
    
    UIView *viewBack = [[UIView alloc] initWithFrame:(CGRect){20,(self.frame.size.height-250)/2,280,250}];
    viewBack.backgroundColor = [UIColor whiteColor];
    viewBack.layer.masksToBounds = YES;
    viewBack.layer.cornerRadius = 10.f;
    [self addSubview:viewBack];
    
    btnAddLogo = [UIButton buttonWithType:UIButtonTypeCustom];
    btnAddLogo.frame = (CGRect){120,10,52,73};
    btnAddLogo.layer.masksToBounds = YES;
    btnAddLogo.layer.cornerRadius = 5.f;
    [btnAddLogo addTarget:self action:@selector(btnClickSelecteImg) forControlEvents:UIControlEventTouchUpInside];
    [btnAddLogo setImage:[UIImage imageNamed:@"btn_lingdao"] forState:UIControlStateNormal];
    [viewBack addSubview:btnAddLogo];
    
    NSArray *aryTitle = @[@"领导职位",@"领导名称"];
    for (int i = 0; i<aryTitle.count; i++) {
        UILabel *labTitle = [[UILabel alloc] initWithFrame:(CGRect){10,105+40*i,60,20}];
        labTitle.text = aryTitle[i];
        labTitle.font = [UIFont systemFontOfSize:14.5f];
        labTitle.textColor = [UIColor colorWithRed:89/255.f green:87/255.f blue:87/255.f alpha:1.f];
        [viewBack addSubview:labTitle];
    }
    
    tf1 = [[UITextField alloc] initWithFrame:(CGRect){80,100,180,30}];
    tf1.placeholder = @"请输入领导职位";
    tf1.layer.masksToBounds = YES;
    tf1.layer.cornerRadius = 5.f;
    tf1.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
    tf1.layer.borderWidth = 0.5f;
    tf1.font = [UIFont systemFontOfSize:12.5f];
    tf1.textColor = [UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f];
    [viewBack addSubview:tf1];
    
    tf2 = [[UITextField alloc] initWithFrame:(CGRect){80,140,180,30}];
    tf2.placeholder = @"请输入领导名称";
    tf2.layer.masksToBounds = YES;
    tf2.layer.cornerRadius = 5.f;
    tf2.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
    tf2.layer.borderWidth = 0.5f;
    tf2.font = [UIFont systemFontOfSize:12.5f];
    tf2.textColor = [UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f];
    [viewBack addSubview:tf2];
    
    
    
    UIButton *btnEnter = [UIButton buttonWithType:UIButtonTypeCustom];
    btnEnter.frame = (CGRect){10,190,260,40};
    btnEnter.backgroundColor = [UIColor colorWithRed:32/255.0f green:165/255.0f blue:240/255.0f alpha:1.0f];
    btnEnter.layer.masksToBounds = YES;
    btnEnter.layer.cornerRadius = 5.f;
    btnEnter.titleLabel.font = [UIFont systemFontOfSize:14.5f];
    [btnEnter setTitle:@"确定" forState:UIControlStateNormal];
    [btnEnter setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnEnter addTarget:self action:@selector(btnClickOK) forControlEvents:UIControlEventTouchUpInside];
    [viewBack addSubview:btnEnter];
    
    [self.mainVC.view addSubview:self];
}

- (void)dismissView
{
    self.hidden = YES;
}

#pragma mark - 选择logo
- (void)btnClickSelecteImg
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"照片选择"
                                                             delegate:self
                                                    cancelButtonTitle:@"取消"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"相机",@"相册", nil];
    [actionSheet showInView:self];
    
}

#pragma mark - 选择照片
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //先设定sourceType为相机，然后判断相机是否可用（ipod）没相机，不可用将sourceType设定为相片库
    UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];
    pickerImage.delegate = self;
    pickerImage.allowsEditing = YES;//设置可编辑
    switch (buttonIndex) {
        case 0:
        {
            NSLog(@"相机");
            pickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self.mainVC presentViewController:pickerImage animated:YES completion:nil];//进入照相界面
        }
            break;
        case 1:
        {
            NSLog(@"相册");
            pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //图片库
            [self.mainVC presentViewController:pickerImage animated:YES completion:nil];//进入照相界面
        }
            break;
        case 2:
        {
            NSLog(@"取消");
        }
            break;
    }
    
}

//点击相册中的图片 货照相机照完后点击use  后触发的方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    
    [btnAddLogo setImage:image forState:UIControlStateNormal];
    imgSelected = image;
}

//点击cancel 调用的方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if (self.superview!=nil) {
        if ([touch.view isKindOfClass:[UIView class]] && touch.view != self) {
            return NO;
        }
    }
    return YES;
}


- (void)btnClickOK
{
    BOOL isOk = YES;
    if (imgSelected == nil) {
        isOk = NO;
        [SVProgressHUD showErrorWithStatus:@"请添加领导照片"];
        [SVProgressHUD dismissafterDelay:kHideHudDelay];
    }
    
    if (isOk==YES && tf1.text.length == 0) {
        isOk = NO;
        [SVProgressHUD showErrorWithStatus:@"请输入领导职位"];
        [SVProgressHUD dismissafterDelay:kHideHudDelay];
    }
    
    if (isOk==YES && tf2.text.length == 0) {
        isOk = NO;
        [SVProgressHUD showErrorWithStatus:@"请输入领导名称"];
        [SVProgressHUD dismissafterDelay:kHideHudDelay];
    }
    
    if (isOk) {
        [self.delegatee AddDelegateWithPosition:tf1.text withLeaderName:tf2.text withLogo:imgSelected];
        self.hidden = YES;
    }
    
    
}



@end
